<template>
  <div class="infinite-container" style="padding-bottom:50px;top:0;">
    <div>
      <q-a :qadata="QAdata" :defaultQadata="defaultQadata"></q-a>
    </div>
  </div>
</template>

<script>
import CommonList from './CommonList'
import QA from './QA'
import {commonNISAjaxGet} from '../../assets/public'
export default {
  components: {CommonList, QA},
  ready () {
    // this.$store.commit('UPDATE_TITLE', '满意度调查')
    this.$store.commit('UPDATE_HEADERSHOW', false)
    this.getStyle()
  },
  data () {
    return {
      QAdata: [],
      defaultQadata: {}
    }
  },
  methods: {
    refactData (arr) {
      let temp = []
      if (arr.length === 0) {
        return temp
      }
      arr.forEach(v => {
        if (v.XJKJLX === 1 || v.XJKJLX === 2) {
          // for text & textarea
          temp.push(this.refactInput(v))
        } else if (v.XJKJLX === 3) {
          // for normal radio
          temp.push(this.refactRadio(v))
        } else if (v.XJKJLX === 5) {
          // for select radio
          temp.push(this.refactSelect(v))
        } else if (v.XJKJLX === 4) {
          // for checkbox
          temp.push(this.refactCheckbox(v))
        } else if (v.XJKJLX === 9) {
          // for item title
          if (v.XMLB > 2) {
            temp.push(this.refactInput(v))
          } else {
            temp.push({
              type: 'title',
              title: v.XMMC,
              show: v.show,
              children: this.refactData(v.children)
            })
          }
        }
      })
      return temp
    },
    refactInput (v) {
      let obj = {
        options: [],
        type: 'input',
        title: v.XMMC,
        show: v.show
      }
      // fix some fuck input elements have no children
      // return current model
      if (v.children.length === 0) {
        obj.options.push(v)
        return obj
      }
      // todo: input
      v.children.forEach(v1 => {
        let tempObj = {
          label: v1.XMMC,
          KJLX: v.XJKJLX,
          value: '',
          model: '',
          type: 'input',
          XMLB: v1.XMLB,  // 1,2 only show texts
          QZWB: v1.QZWB, // before input's texts
          HZWB: v1.HZWB, // after input's texts
          SJGS: v1.SJGS, // format texts
          XMXH: v1.XMXH
        }
        if (v1.XJKJLX !== 9) {
          tempObj.children = this.refactData([v1])
        }
        obj.options.push(tempObj)
      })
      return obj
    },
    refactRadio (v) {
      // todo: radio
      let obj = {
        options: [],
        KJLX: v.XJKJLX,
        XMXH: v.XMXH,
        SJXM: v.SJXM,
        type: 'radio',
        title: v.XMMC,
        model: '',
        show: v.show
      }
      v.children.forEach(v1 => {
        let tempObj = {
          label: v1.XMMC,
          value: String(v1.PLSX)
        }
        if (v1.XJKJLX === 4) {
          // fix checkbox
          tempObj.children = this.refactData([v1])
        } else if (v1.XJKJLX !== 9) {
          tempObj.children = this.refactData(v1.children)
        }
        obj.options.push(tempObj)
      })
      return obj
    },
    refactSelect (v) {
      // todo: select
      let options = []
      var xmkz = v.children[0].XMKZ
      xmkz = xmkz.slice(1)
      let arr = xmkz.split(';')
      // fixed bug: remove last array element
      // Array.pop
      if (!arr[arr.length - 1]) {
        arr.pop()
      }
      arr.forEach(v1 => {
        options.push({
          label: v1.split(',')[0],
          value: String(v1.split(',')[1])
        })
      })
      let obj = {
        KJLX: v.XJKJLX,
        XMXH: v.XMXH,
        SJXM: v.SJXM,
        type: 'radio',
        title: v.XMMC,
        model: '',
        show: v.show,
        options: options
      }
      return obj
    },
    refactCheckbox (v) {
      // todo: checkbox
      let obj = {
        options: [],
        KJLX: v.XJKJLX,
        XMXH: v.XMXH,
        SJXM: v.SJXM,
        type: 'checkbox',
        title: v.XMMC,
        show: v.show,
        model: []
      }
      // todo: input
      v.children.forEach(v1 => {
        let tempObj = {
          label: v1.XMMC,
          value: String(v1.PLSX)
        }
        if (v1.XJKJLX !== 9) {
          tempObj.children = this.refactData(v1.children)
        }
        obj.options.push(tempObj)
      })
      return obj
    },
    getStyle () {
      var path = this.$route.fullPath.split('?')[1]
      var params = '?JGID=1&' + path
      this.$store.commit('UPDATE_LOADING')
      commonNISAjaxGet(`get/style${params}`).then(res => {
        this.$store.commit('UPDATE_LOADING')
        if (res.data.ReType === 0 && res.data.Data) {
          this.$store.commit('UPDATE_TITLE', res.data.Data[0].YSMC)
          this.defaultQadata = res.data.Data[0]
          // var data = res.data.Data[0].projects.slice(1, 4)
          var data = res.data.Data[0].projects
          data.forEach((v, i) => {
            v.show = (i === 0)
          })
          this.QAdata = this.refactData(data)
        } else {
          this.$store.commit('UPDATE_TOAST', {
            message: res.data.Msg
          })
          this.$router.push('/login?redirect=/flowUp')
        }
      })
    }
  }
}
</script>

<style lang="less">

</style>
